﻿using Microsoft.AspNetCore.Mvc;
using ProTrack.Api.Helper;
using ProTrack.Dto;
using ProTrack.Entity;
using ProTrack.IServices.Examine;
using ProTrack.Report;
using ProTrack.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProTrack.Api.Controllers
{
    /// <summary>
    /// 常见缺陷控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class QcProjectLevelController : Controller
    {
        /// <summary>
        /// 日志收集
        /// </summary>
        /// <param name="services"></param>
        /// <param name="LogHelper"></param>
        public QcProjectLevelController(IQcProjectLevelServices services, LogHelper LogHelper)
        {
            Services = services;
            this.LogHelper = LogHelper;
        }

        public IQcProjectLevelServices Services { get; }
        public LogHelper LogHelper { get; }


        /// <summary>
        /// 获取所有常见缺陷
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetAll")]
        public async Task<IActionResult> GetAll()
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var list = await Services.GetAllAsync();
                status.Data = list;
                status.Count = list.Count();
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("获取所有常见缺陷", ex, HttpContext);
            }
            return Ok(status);
        }


        /// <summary>
        /// 分页获取常见缺陷
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="Remark"></param>
        /// <param name="LevelName"></param>
        /// <param name="Classify"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get([FromQuery] int PageIndex, [FromQuery] int PageSize, [FromQuery] string Remark, [FromQuery] string LevelName, [FromQuery] string Classify)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                Remark = Remark ?? "";
                LevelName = LevelName ?? "";
                Classify = Classify ?? "";
                var list = await Services.GetAsync(PageIndex, PageSize, Remark, LevelName,Classify);
                status.Data = list;
                status.Count = await Services.GetCountAsync(Remark, LevelName, Classify);
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("常见缺陷-分页获取", ex, HttpContext);
            }
            return Ok(status);

        }



        /// <summary>
        /// 新增常见缺陷
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add([FromForm] QcProjectLevel model)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                //获取当前时间段创建时间
                model.CreateTime = StringHelper.GetTimeNow();
                model.Creator = User.Identity.Name;
                var isSuccess = await Services.AddAsync(model);
                if (isSuccess)
                {
                    status.Msg = "新增成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "新增失败，请检查数据。";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("常见缺陷-新增", ex, HttpContext);
            }
            return Ok(status);

        }



        /// <summary>
        /// 修改常见缺陷
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> Update([FromForm] QcProjectLevel model)
        {
            var status = BaseStatus.CreateInstance();

            try
            {
                //获取当前修改的时间
                model.UpdateTime = StringHelper.GetTimeNow();
                model.Updater = User.Identity.Name;
                var isSuccess = await Services.UpdateAsync(model);
                if (isSuccess)
                {
                    status.Msg = "修改成功";

                    //操作日志
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "修改失败，请检查数据。";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("常见缺陷-修改", ex, HttpContext);
            }
            return Ok(status);

        }

        /// <summary>
        /// 批量删除常见缺陷
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        [HttpDelete("DelAll")]
        public async Task<IActionResult> DelAll([FromForm] string Ids)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var arr = Ids.SplitId();
                var isSuccess = await Services.DeleteAsync(arr);
                if (isSuccess)
                {
                    status.Msg = "删除成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "删除失败，请稍后重试。";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("常见缺陷-批量删除", ex, HttpContext);
            }
            return Ok(status);
        }


        /// <summary>
        /// 删除常见缺陷数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> Del([FromForm] int Id)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var isSuccess = await Services.DeleteAsync(Id);
                if (isSuccess)
                {
                    status.Msg = "删除成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "删除失败，请稍后重试。";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("常见缺陷-删除", ex, HttpContext);
            }

            return Ok(status);
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        [HttpGet("Export")]
        public async Task<IActionResult> Export()
        {
            try
            {
                var list = await Services.GetAllAsync();
                var TitleArray = new ReportDomain[] {
                    new ReportDomain{ Title="缺陷描述",Fileld="Remark"},
                    new ReportDomain{ Title="检测项类型",Fileld="Classify"},
                    new ReportDomain{ Title="缺陷等级",Fileld="LevelName"},
                    new ReportDomain{ Title="创建时间",Fileld="CreateTime"},
                };
                var fileArray = BaseReport.Export(list, "常见缺陷信息", TitleArray);

                return File(new System.IO.MemoryStream(fileArray), "application/octet-stream", "常见缺陷信息.xls");
            }
            catch (Exception ex)
            {

                //此处应当日志记录
                await LogHelper.WriteErrorLog("常见缺陷信息-导出", ex, HttpContext);
                return new JsonResult(BaseStatus.CreateInstance(-1, "导出失败，服务器异常请稍后再试！"));
            }
        }



    }
}
